package com.gree.scmservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.gree.common.annotation.Log;
import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.SysLogininfor;
import com.gree.common.entity.TablePageData;
import com.gree.common.enums.BusinessType;
import com.gree.common.utils.ExcelUtil;
import com.gree.common.utils.StringUtils;
import com.gree.framework.entity.CusUserDetails;
import com.gree.framework.entity.SysUser;
import com.gree.framework.entity.SysUserEx;
import com.gree.framework.utils.SecurityUtils;
import com.gree.scmservice.dao.LogInfoDao;
import com.gree.scmservice.service.GscmSysLogininforService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Record;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.cri.SqlExpression;
import org.nutz.dao.util.cri.Static;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 登录日志
 * Created by 180686 on 2020/11/17 14:48
 */

@RestController
@Api(tags = "系统信息:登录日志接口")
@RequestMapping("/loginlog")
public class LoginlogController {

    @Autowired
    private GscmSysLogininforService gscmSysLogininforService;

    @Autowired
    Dao dao;

    /**
     * 导出登录日志
     * @param logininfor
     * @return
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出登录日志")
    @ApiImplicitParam(name = "logininfor", value = "用户登录日志", dataType = "SysLogininfor")
    public void export(SysLogininfor logininfor, HttpServletResponse response){
        TablePageData<SysLogininfor> list = gscmSysLogininforService.selectLogininforList(logininfor);
        ExcelUtil<SysLogininfor> util = new ExcelUtil<SysLogininfor>(SysLogininfor.class);
        try {
            util.exportExcel(list.getDataList(), "登录日志", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除登录日志
     * @param infoIds 登录日志ID
     * @return
     */
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    @ApiOperation(value = "删除登录日志")
    @ApiImplicitParam(name = "infoIds", value = "用户登录日志ID", dataType = "Long[]")
    public Object remove(@PathVariable Long[] infoIds){
        int resultNum = gscmSysLogininforService.deleteLogininforByIds(infoIds);
        return ReturnVo.toAjax(resultNum);
    }

    /**
     * 清空登录日志
     * @return
     */
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @ApiOperation(value = "清空登录日志")
    @DeleteMapping("/clean")
    public Object clean(){
        gscmSysLogininforService.cleanLogininfor();
        return ReturnVo.success();
    }

    /**
     * 分页获取登录日志
     * @param jsonObject
     * @return
     */
    @PostMapping("/getlog")
    @ApiOperation(value = "分页获取登录日志")
    @ApiImplicitParam(name = "infoIds", value = "用户登录日志ID", dataType = "Long[]")
    public Object getloginlog(@RequestBody JSONObject jsonObject){
        TablePageData<Record> recordTablePageData = gscmSysLogininforService.selectLogininforList(jsonObject);
        return ReturnVo.success(recordTablePageData);
    }

}
